/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024.
 * Description: EulerOS fastsched trace Header
 * Create: 2024-06-15
 */

#ifndef TRACE_EVENTS_EULEROS_FASTSCHED_H
#define TRACE_EVENTS_EULEROS_FASTSCHED_H

#ifdef CONFIG_EULEROS_FAST_SCHED
#include <linux/sched/numa_balancing.h>
#include <linux/tracepoint.h>
#include <linux/binfmts.h>

TRACE_EVENT(fast_sched_ctx_adjust,

	TP_PROTO(struct task_struct *cur_tsk,
		bool enqueue,
		int cur_cpu,
		int flags),

	TP_ARGS(cur_tsk, enqueue, cur_cpu, flags),

	TP_STRUCT__entry(
		__array(char,	tsk_comm,	TASK_COMM_LEN)
		__field(pid_t,	tsk_pid)
		__field(int,	tsk_cpu)
		__field(bool,	enqueue)
		__field(int,	cur_cpu)
		__field(int,	flags)
	),

	TP_fast_assign(
		memcpy(__entry->tsk_comm, cur_tsk->comm, TASK_COMM_LEN);
		__entry->tsk_pid	= cur_tsk->pid;
		__entry->tsk_cpu	= task_cpu(cur_tsk);
		__entry->enqueue	= enqueue;
		__entry->cur_cpu	= cur_cpu;
		__entry->flags		= flags;
	),

	TP_printk("comm=%s pid=%d cpu%d %s cpu%d flags:%x",
		__entry->tsk_comm, __entry->tsk_pid, __entry->tsk_cpu,
		__entry->enqueue ? "enqueue to" : "dequeue from",
		__entry->cur_cpu, __entry->flags)
);

TRACE_EVENT(fast_sched_pick_next_entity,

	TP_PROTO(struct task_struct *next,
		struct task_struct *second,
		struct task_struct *curr),

	TP_ARGS(next, second, curr),

	TP_STRUCT__entry(
		__array(char,	nx_tsk_comm,	TASK_COMM_LEN)
		__field(pid_t,	nx_tsk_pid)
		__array(char,	se_tsk_comm,	TASK_COMM_LEN)
		__field(pid_t,	se_tsk_pid)
		__array(char,	cur_tsk_comm,	TASK_COMM_LEN)
		__field(pid_t,	cur_tsk_pid)
	),

	TP_fast_assign(
		memcpy(__entry->nx_tsk_comm, next ? next->comm : "null", TASK_COMM_LEN);
		__entry->nx_tsk_pid		= next ? next->pid : -1;
		memcpy(__entry->se_tsk_comm, second ? second->comm : "null", TASK_COMM_LEN);
		__entry->se_tsk_pid		= second ? second->pid : -1;
		memcpy(__entry->cur_tsk_comm, curr ? curr->comm : "null", TASK_COMM_LEN);
		__entry->cur_tsk_pid	= curr ? curr->pid : -1;
	),

	TP_printk("next [%s %d] second [%s %d] curr [%s %d]",
		__entry->nx_tsk_comm, __entry->nx_tsk_pid, __entry->se_tsk_comm,
		__entry->se_tsk_pid, __entry->cur_tsk_comm, __entry->cur_tsk_pid)
);

TRACE_EVENT(fast_sched_wakeup,

	TP_PROTO(struct task_struct *p,
		int result,
		bool curr),

	TP_ARGS(p, result, curr),

	TP_STRUCT__entry(
		__array(char,	comm,	TASK_COMM_LEN)
		__field(pid_t,	pid)
		__field(int,	result)
		__field(bool,	curr)
	),

	TP_fast_assign(
		memcpy(__entry->comm, p ? p->comm : "null", TASK_COMM_LEN);
		__entry->pid		= p ? p->pid : -1;
		__entry->result		= result;
		__entry->curr		= curr;
	),

	TP_printk("wakeup [%s %d] success:%d current:%d",
		__entry->comm, __entry->pid, __entry->result, __entry->curr)
);

TRACE_EVENT(fast_sched_wait,

	TP_PROTO(struct task_struct *p,
		bool start),

	TP_ARGS(p, start),

	TP_STRUCT__entry(
		__array(char,	comm,	TASK_COMM_LEN)
		__field(pid_t,	pid)
		__field(bool,	start)
	),

	TP_fast_assign(
		memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
		__entry->pid	= p->pid;
		__entry->start	= start;
	),

	TP_printk("[%s %d] wait %s",
		__entry->comm, __entry->pid, __entry->start ? "prepare" : "finish")
);

TRACE_EVENT(fast_sched_idle,

	TP_PROTO(bool need_sched,
		unsigned int nr_running),

	TP_ARGS(need_sched, nr_running),

	TP_STRUCT__entry(
		__field(bool,	need_sched)
		__field(unsigned int,	nr_running)
	),

	TP_fast_assign(
		__entry->need_sched	= need_sched;
		__entry->nr_running	= nr_running;
	),

	TP_printk("fast_sched_idle out [%d %d]",
		__entry->need_sched, __entry->nr_running)
);

/* Trace file need include again */
#undef TRACE_EVENTS_EULEROS_FASTSCHED_H
#endif

#endif
